﻿using System;
using System.IO;
using System.Windows.Forms;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Server;

namespace WITSynchronizer
{
	/// <summary>
	/// Form to let the user duplicate a Process Template
	/// </summary>
	public partial class DuplicateProcessTemplateForm : Form
	{
		/// <summary>
		/// The Team Foundation Server to use
		/// </summary>
		TeamFoundationServer		TFS;

		/// <summary>
		/// Constructor
		/// Create the form instance, use the given Team Foundation Server
		/// </summary>
		/// <param name="tfs">The Team Foundation Server object to use</param>
		public DuplicateProcessTemplateForm(TeamFoundationServer tfs)
		{
			// Copy the object
			TFS = tfs;

			// Initialize the form
			InitializeComponent();

			// Get the Process Template interface and setup the combolist with all the Team Projects
			IProcessTemplates ptp = (IProcessTemplates)TFS.GetService(typeof(IProcessTemplates));

			TemplateHeader[] headers = ptp.TemplateHeaders();
			foreach (TemplateHeader header in headers)
			{
				ProcessTemplateList.Items.Add(header.Name);
			}			

			// Disable the "Ok" button as the new Process Template name is invalid...
			OkButton.Enabled = false;
		}

		/// <summary>
		/// Event handler when the duplicated Process Template name changes
		/// </summary>
		/// <param name="sender">Sender object</param>
		/// <param name="e">Event arguments</param>
		private void ProcessTemplateName_TextChanged(object sender, EventArgs e)
		{
			// If there's some text enable the "Ok" button, disable it otherwise
			OkButton.Enabled = (ProcessTemplateName.Text.Length > 0);
		}

		/// <summary>
		/// Event handler when the form is closed
		/// </summary>
		/// <param name="sender">Sender object</param>
		/// <param name="e">Event arguments</param>
		private void DuplicateProcessTemplate_FormClosed(object sender, FormClosedEventArgs e)
		{
			// Only do the operation if the user clicked "Ok"
			if (DialogResult != System.Windows.Forms.DialogResult.OK)		return;

			// Get the template to duplicate
			String srcptname = ProcessTemplateList.SelectedItem as String;

			// Get the name of the new template
			String dstptname = ProcessTemplateName.Text;

			// Check if the new name already exists
			IProcessTemplates ptp = (IProcessTemplates)TFS.GetService(typeof(IProcessTemplates));

			// Loop through all the Process Template, check if the name the user choosed is already taken and get the header of the source Process Template
			TemplateHeader srcheader = null;
			TemplateHeader[] headers = ptp.TemplateHeaders();
			foreach (TemplateHeader header in headers)
			{
				if (header.Name == dstptname)
				{
					MessageBox.Show("A Process Template of the same name already exists on the server!", "WIT Synchronizer", MessageBoxButtons.OK, MessageBoxIcon.Error);
					return;
				}

				if (header.Name == srcptname)
				{
					srcheader = header;
				}
			}

			// Add the new template
			int pti = ptp.GetTemplateIndex(srcptname);
			String srcpfn = ptp.GetTemplateData(pti);

			ptp.AddTemplate(dstptname, DescriptionText.Text, srcheader.Metadata, "visible");
			ptp.AddUpdateTemplate(dstptname, DescriptionText.Text, srcheader.Metadata, "visible", srcpfn);

			// Delete the zip file we got
			File.Delete(srcpfn);

			// Display success messagebox
			MessageBox.Show("Process Template duplicated successfully", "WIT Synchronizer", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
		}
	}
}
